package com.llb666.shop.order.infracore.mq;

import com.llb666.common.core.enums.OrderStatusEnum;
import com.llb666.common.core.utils.DateUtil;
import com.llb666.shop.order.domain.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.util.function.Consumer;

/**
 * 订单服务rabbitMq接受消息.
 *
 * @author llb666
 */
@Component
@Slf4j
public class RabbitMqReceive {

    private final OrderService orderService;

    public RabbitMqReceive(OrderService orderService) {
        this.orderService = orderService;
    }

    /**
     * 取消超时订单.
     *
     * @return Consumer
     */
    @Bean
    public Consumer<Message<String>> delay() {
        return obj -> {
            String orderId = obj.getPayload();
            log.info("delayOrder-消息接收成功：目前时间为{}，需要取消订单{}", DateUtil.dateToStringWithTime(), orderId);
            // 需要幂等处理，不重复更新，不错误更新，只更新'待付款'的订单状态.
            orderService.updateOrderStatusByWaitPay(orderId, OrderStatusEnum.CLOSE.type);
        };
    }
}
